﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.Ajax
{
    public partial class Care : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string frienduserid = Request.QueryString["frienduserid"] + "";
            Users uinfo = UserBase.GetCurrentUser();
            if (uinfo == null)
            {
                Response.Write("请先登录");
                return;
            }
            else
            {
                if (frienduserid == uinfo.ID)
                {
                    Response.Write("不能关注自己");
                    return;
                }
                else
                {
                    //判断是否已经关注过
                    FriendHelper fhelper = new FriendHelper();
                    Criteria c = new Criteria();
                    c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                    c.Add(CriteriaType.Equals, "FriendUsersID", frienduserid);
                    int count = fhelper.QueryFriendCountBySearch(c);
                    if (count > 0)
                    {
                        Response.Write("已经关注过");
                        return;
                    }
                    else
                    {
                        //查找该用户的昵称
                        Users friendinfo = new Users();
                        UsersHelper uhelper = new UsersHelper();
                        friendinfo = uhelper.GetUsers(frienduserid);

                        Friend finfo = new Friend();

                        finfo.FriendUsersID = frienduserid;
                        finfo.FriendNickName = friendinfo.NickName;
                        finfo.UsersID = uinfo.ID;
                        finfo.NickName = uinfo.NickName;
                        finfo.Type = 1;

                        fhelper.AddFriend(finfo);

                        //增加被关注用户表中friendnum的数量
                        int tobefriendnum = friendinfo.ToBeFriendNum;
                        tobefriendnum = tobefriendnum + 1;
                        friendinfo.ToBeFriendNum = tobefriendnum;
                        uhelper.EditUsers(friendinfo);

                        //增加当前用户关注的数量
                        int friendnum = uinfo.FriendNum;
                        friendnum = friendnum + 1;
                        uinfo.FriendNum = friendnum;
                        uhelper.EditUsers(uinfo);

                        Response.Write("关注成功");
                        return;
                    }
                }
            }
        }
    }
}